// pages/order-detail/order-detail.js
var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
var token = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0,  //当前登录用户Id
    token: '', //goAtherPagegoAtherPage
    order: {},
    orderid : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var inviteuid = options.inviteuid;

    console.log(options.orderid );

    this.setData({    
      orderid: options.orderid    
    })   
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      //更新数据
      if (res && res != -1) {
      that.setData({
        uid: res.uid,
        token: res.token
      })
      uid = res.uid;
      token = res.token;
      //获取订单
      that.getOrder(uid, token);
      }
    }, inviteuid);  
  
  },

  getOrder:function(uid, token){
    var that = this;
    var privatekey = utilMd5.hexMD5(uid + 'ordercontent' + token);
    util.api({
      data: {
        publickey: util.publiccode,
        ac: "ordercontent",
        uid: uid,
        privatekey: privatekey,
        orderid: that.data.orderid
      },
      method: 'GET',
      success: function (responseData) {
        console.log(responseData);
        var data = responseData.data;
        data.addtime = util.formatTime(new Date(data.addtime * 1000));
        if (data.paytime){
          data.paytime = util.formatTime(new Date(data.paytime * 1000));
        }
        if (data.sendtime) {
          data.sendtime = util.formatTime(new Date(data.sendtime * 1000));
        }
        if (data.sendtime) {
          data.rectime = util.formatTime(new Date(data.rectime * 1000));
        }
        if (data.huiping && data.huiping.dateline ){
          data.huiping.dateline = util.formatTime(new Date(data.huiping.dateline * 1000));
        }
        if (data.zhuiping && data.zhuiping.dateline) {
          data.zhuiping.dateline = util.formatTime(new Date(data.zhuiping.dateline * 1000));
        }

        that.setData({
          order: data
        })

        console.log(responseData)
      }
    });   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})